<script>
import ZPMixin from '@/uni_modules/z-paging/components/z-paging/js/z-paging-mixin';
import { getGoodsList } from '@/api/mall.js';

export default {
  name: "zone",
  mixins: [ZPMixin],
  data() {
    return {
      loading: false,
      keyword: "", // 搜索关键词
      products: [], // 商品列表
      searchParams: {
        Index: 1,
        Size: 10,
        ProductName: "", // 搜索关键词
        Tags: "", // 搜索关键词
        TypeId: 2,
        PurchaseMethod: "0"
      },
      zoneName: "" // 分区名称
    }
  },
  onLoad(options) {
    if (options.id) {
      this.searchParams.Tags = options.id;
    }
    if (options.title) {
      this.zoneName = decodeURIComponent(options.title);
      uni.setNavigationBarTitle({
        title: this.zoneName || '专区商品'
      });
    }
  },
  methods: {
    // 商品列表查询
    async queryList(pageNo, pageSize) {
      try {
        this.loading = true;
        this.searchParams.Index = pageNo;
        this.searchParams.Size = pageSize;
        this.searchParams.ProductName = this.keyword;

        const { Data } = await getGoodsList(this.searchParams);

        this.loading = false;
        this.$refs.paging.complete(Data || []);
      } catch (error) {
        console.error("获取商品列表失败:", error);
        this.loading = false;
        this.$refs.paging.complete(false);
      }
    },

    // 搜索
    onSearch() {
      this.$refs.paging.reload();
    },

    // 点击商品
    goToProductDetail(id) {
      uni.navigateTo({
        url: `/mall_subpages/goods/index?id=${id}&type=1`
      });
    }
  }
}
</script>

<template>
  <view class="zone-container">
    <z-paging
        ref="paging"
        v-model="products"
        @query="queryList"
        :refresher-enabled="true"
        :auto-show-back-to-top="true"
        :auto-scroll-to-top-when-reload="true"
        :show-scrollbar="false"
        :paging-style="{height: '100%'}"
    >
      <!-- 顶部搜索区域 -->
      <template #top>
        <view class="search-container">
          <view class="search-box">
            <view class="search-input-wrapper">
              <u-icon name="search" color="#999" size="30"></u-icon>
              <input
                type="text"
                v-model="keyword"
                placeholder="搜索商品"
                confirm-type="search"
                @confirm="onSearch"
              />
              <u-icon name="close" color="#999" size="20" v-if="keyword" @click="keyword = ''; onSearch()"></u-icon>
            </view>
            <view class="search-btn" @click="onSearch">搜索</view>
          </view>
        </view>
      </template>

      <!-- 商品列表 -->
      <view class="product-list">
        <view
            class="product-item"
            v-for="(item, index) in products"
            :key="index"
            @click="goToProductDetail(item.Id)"
        >
          <view class="product-image">
            <image :src="item.Img" mode="aspectFill"></image>
          </view>
          <view class="product-info">
            <view class="product-name">{{item.Name || '商品名称'}}</view>
            <view class="product-price-row">
              <text class="product-price">¥{{item.ShowPrice || 0}}</text>
              <text class="product-sales">{{item.SalesCount || 0}}人购买</text>
            </view>
          </view>
        </view>
      </view>
    </z-paging>
  </view>
</template>

<style scoped lang="scss">
.zone-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #F5F6F7;
}

/* 搜索区域样式 */
.search-container {
  background-color: #ffffff;
  padding: 20rpx 30rpx;
  position: relative;
  z-index: 10;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.search-box {
  display: flex;
  align-items: center;

  .search-input-wrapper {
    flex: 1;
    display: flex;
    align-items: center;
    background-color: #F5F6F7;
    border-radius: 40rpx;
    padding: 10rpx 20rpx;
    height: 70rpx;

    input {
      flex: 1;
      height: 70rpx;
      font-size: 28rpx;
      color: #333;
      margin: 0 16rpx;
    }
  }

  .search-btn {
    margin-left: 20rpx;
    font-size: 28rpx;
    color: #333;
    height: 70rpx;
    line-height: 70rpx;
    padding: 0 20rpx;
  }
}

/* 商品列表样式 */
.product-list {
  padding: 20rpx;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.product-item {
  width: 48%;
  background-color: #ffffff;
  margin-bottom: 20rpx;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05);

  .product-image {
    width: 100%;
    height: 320rpx;

    image {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .product-info {
    padding: 16rpx;

    .product-name {
      font-size: 28rpx;
      color: #333;
      line-height: 1.3;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      height: 74rpx;
    }

    .product-price-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 10rpx;

      .product-price {
        font-size: 32rpx;
        color: #FF4B31;
        font-weight: bold;
      }

      .product-sales {
        font-size: 24rpx;
        color: #999;
      }
    }
  }
}

/* 空数据显示样式 */
.empty-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 100rpx;

  image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
  }

  text {
    font-size: 28rpx;
    color: #999;
  }
}

/* 加载动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.product-item {
  animation: fadeIn 0.5s ease-out;
}
</style>
